<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $use_id
 * @property string $use_password
 * @property string $use_name
 * @property string $use_email
 * @property string $use_address
 * @property string $use_phone
 * @property integer $use_active
 */
class Users extends CActiveRecord
{
	public $error = array('errorCode' => 0,
                         'errorMessage' => '');
   /**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('use_active', 'numerical', 'integerOnly'=>true),
			array('use_password, use_name, use_email, use_address, use_phone', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('use_id, use_password, use_name, use_email, use_address, use_phone, use_active', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'use_id' => 'Use',
			'use_password' => 'Use Password',
			'use_name' => 'Use Name',
			'use_email' => 'Use Email',
			'use_address' => 'Use Address',
			'use_phone' => 'Use Phone',
			'use_active' => 'Use Active',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('use_id',$this->use_id);
		$criteria->compare('use_password',$this->use_password,true);
		$criteria->compare('use_name',$this->use_name,true);
		$criteria->compare('use_email',$this->use_email,true);
		$criteria->compare('use_address',$this->use_address,true);
		$criteria->compare('use_phone',$this->use_phone,true);
		$criteria->compare('use_active',$this->use_active);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
   
   public function register($username,$fullname,$email,$password,$repassword,$address,$phone,$active = 1){
      //Check exits
      $db = Yii::app()->db;
      $sql = "SELECT use_id FROM " . $this->tableName() . " WHERE use_username =:username";
      $cmd = $db->createCommand($sql);
      $cmd->bindParam(":username",$username);
      $exists = $cmd->queryScalar();
      if(!$exists){
         $sql = "SELECT use_id FROM " . $this->tableName() . " WHERE use_email =:email";
         $cmd = $db->createCommand($sql);
         $cmd->bindParam(":email",$email);
         $exists = $cmd->queryScalar();
         if(!$exists){
            if($password == $repassword){
               $password = md5($password);
               $sql = "INSERT INTO " . $this->tableName(). "(use_username,use_fullname,use_password,use_email,use_address,use_phone,use_active) 
                      VALUES(:username,:fullname,:password,:email,:address,:phone,:active)";
               $cmd = $db->createCommand($sql);
               $cmd->bindParam(":username",$username);
               $cmd->bindParam(":fullname",$fullname);
               $cmd->bindParam(":password",$password);
               $cmd->bindParam(":email",$email);
               $cmd->bindParam(":address",$address);
               $cmd->bindParam(":phone",$phone);
               $cmd->bindParam(":active",$active);
               $insert = $cmd->execute();
               if($insert)
                  return true;
               else
                  return false;
            }else{
               $this->error['errorCode'] = 1;
               $this->error['errorMessage'] = "Xác nhận mật khẩu không chính xác";
               return false;
            }
         }else{
            $this->error['errorCode'] = 1;
            $this->error['errorMessage'] = "Email đã có người khác sử dụng";
            return false;
         }
      }else{
         $this->error['errorCode'] = 1;
         $this->error['errorMessage'] = "Tên đăng nhập đã có người khác sử dụng";
         return false;
      }
      
   }
}
